package sicau.carManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sicau.carManager.entity.Notice;
import sicau.carManager.mapper.NoticeMapper;
import sicau.carManager.service.NoticeService;


import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ITcz
 * @since 2020-07-28
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    NoticeMapper noticeMapper;


    @Override
    public boolean addNotice(String title, String content) {
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setContent(content);
        notice.setTime(LocalDate.now());
        int is = noticeMapper.insert(notice);
        return is==0?false:true;
    }


    @Override
    public boolean deleteNotice(int id) {
        //判断是否删除成功
        boolean is;
        if(noticeMapper.deleteById(id) != 0) {
            is = true;
        }else {
            is = false;
        }
        return is;
    }

    @Override
    public IPage<Notice> getAllNotice(int page, int pageSize) {
        QueryWrapper<Notice> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        Page<Notice> p =new Page<>(page, pageSize);
        IPage<Notice> noticeIPage = noticeMapper.selectPage(p,queryWrapper);
        return noticeIPage;
    }
}
